#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
#include <vector>
using ll = int64_t;

ll n,ans;
std::vector<std::vector<ll>> apos,bpos;
std::vector<ll> a,b;

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n;

    a.resize(n+1);
    b.resize(n+1);
    apos.resize(n+1);
    bpos.resize(n+1);

    for(ll i=1;i<=n;i++){
        std::cin>>a[i];
        apos[a[i]].push_back(std::min(i,n-i+1));
    }
    for(ll i=1;i<=n;i++){
        std::cin>>b[i];
        bpos[b[i]].push_back(std::min(i,n-i+1));
    }
    for(ll i=1;i<=n;i++){
        if(a[i]==b[i]){
            ll N=i-1;
            ans+=N*(N+1)/2;
            N=n-i;
            ans+=N*(N+1)/2;
        }
        std::sort(apos[i].begin(),apos[i].end());
        std::sort(bpos[i].begin(),bpos[i].end());

        ll pa=0,pb=0;
        while(pa<apos[i].size() && pb<bpos[i].size()){
            if(apos[i][pa]<bpos[i][pb]){
                ans+=apos[i][pa++]*(bpos[i].size()-pb);
            }else{
                ans+=bpos[i][pb++]*(apos[i].size()-pa);
            }
        }
    }

    std::cout<<ans<<"\n";
}